/******************************************************************************
* File description: CustomComboColorPicker.qml
* Function: 图标按钮
* Current Version: 1.0.0
 *****************************************************************************/

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtGraphicalEffects 1.0

Item {


    id: root

    // 点击信号
    signal clicked(var mouseX, var mouseY)
    signal released(var mouseX, var mouseY)
    signal pressed(var mouseX, var mouseY)

    signal clicked_disabled(var mouseX, var mouseY)    // 按钮禁用时触发
    signal released_disabled(var mouseX, var mouseY)

    property string title: ""           // 悬浮时触发文字显示

    // 按钮尺寸
    width: 30*dp
    height: 30*dp

    property bool editable: true
    property bool isSelected: false         // 是否为选中状态 (选中状态为true时切换为[选中时样式])

    property string button_icon: ""                         // 图标 (默认无图标)
    property string icon_color: ""

    // 选中时样式
    property string button_icon_select: button_icon
    property string icon_color_select: icon_color

    // 悬浮时样式
    property string button_icon_hover: button_icon
    property string icon_color_hover: icon_color

    // 点击时样式
    property string button_icon_press: button_icon
    property string icon_color_press: icon_color

    // 禁用时样式
    property string button_icon_disable: button_icon
    property string icon_color_disable: icon_color


    // private ======================================================================================
    property bool isPressed: false
    property bool isHovered: false



    // 按钮图标
    Image {
        id: idImage
        anchors.fill: parent
        source: {
            if(!editable){
                return button_icon_disable
            }else if(isSelected){
                return button_icon_select
            }else if(isPressed){
                return button_icon_press
            }else if(isHovered){
                return button_icon_hover
            }else{
                return button_icon
            }
        }
        mipmap: true
        ColorOverlay{
            visible: icon_color!==""
            anchors.fill: parent
            source: parent
            color: {
                if(!editable){
                    return icon_color_disable
                }else if(isSelected){
                    return icon_color_select
                }else if(isPressed){
                    return icon_color_press
                }else if(isHovered){
                    return icon_color_hover
                }else{
                    return icon_color
                }
            }
        }
    }


    MouseArea{
        anchors.fill: parent
        cursorShape: Qt.PointingHandCursor
        hoverEnabled: config.hoverEnabled
        onEntered: {
            isHovered = true && config.hoverEnabled
        }onExited: {
            isHovered = false
        }
        onPressed: {
            isPressed = true
            root.pressed(mouseX, mouseY)
        }
        onReleased: {
            root.focus = true
            isPressed = false
            if (editable){
                root.released(mouseX, mouseY)
            }else{
                root.released_disabled(mouseX, mouseY)
            }
        }
        onClicked: {
            root.focus = true
            if (editable){
                root.clicked(mouseX, mouseY)
            }else{
                root.clicked_disabled(mouseX, mouseY)
            }
        }
    }

    CustomTooltip{
        text: title
        visible: isHovered && title !== ""
    }

}
